/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Client implements Runnable{

    String myIdentity;

    public Client(String pIdentity) {
        myIdentity = pIdentity;
    }

    public void talkToServer() throws InterruptedException {

        try {

            SocketChannel mySocket = SocketChannel.open();

            mySocket.configureBlocking(false);

            mySocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 9999));

            Selector selector = Selector.open();

            mySocket.register(selector, SelectionKey.OP_CONNECT);

            while (selector.select() > 0) {

                Set keys = selector.selectedKeys();
                Iterator it = keys.iterator();

                while (it.hasNext()) {
                    SelectionKey key = (SelectionKey) it.next();

                    SocketChannel myChannel = (SocketChannel) key.channel();

                    it.remove();

                    if (key.isConnectable()) {
                        if (myChannel.isConnectionPending()) {
                            myChannel.finishConnect();
                            System.out.println("Connection was pending but now is finiehed connecting.");
                        }

                        System.out.println("Connected success");
                        
                        mySocket.register(selector, SelectionKey.OP_READ);
                    }
                    else if (key.isReadable()) {
                        ByteBuffer bb = ByteBuffer.allocate(1024);

                        myChannel.read(bb);

                        bb.flip();
                        byte[] array = new byte[bb.limit()];
                        bb.get(array);
                        System.out.println(new String(array) + ". My number is " + myIdentity);
                        
                        mySocket.register(selector, SelectionKey.OP_WRITE);
                    }
                    else if (key.isWritable()) {
                        ByteBuffer bb = null;

                        bb = ByteBuffer.wrap(new String("Message from client").getBytes());
                        myChannel.write(bb);
                        bb.clear();
                        
                        mySocket.register(selector, SelectionKey.OP_READ);
                    }
     
                }
            }
        } catch (IOException e) {
            //e.printStackTrace();
        }
    }

 public static void main(String[] args) throws InterruptedException {

  Client client = new Client(args[0]);
  client.talkToServer();
 }
 
 public void run() {
     try {
         talkToServer();
     } catch (InterruptedException ex) {
         ex.printStackTrace();
     }
 }

}
